﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.GERP.GAMS.MODEL;
using KPIS.DBM;
using System.Configuration;
using System.Data;

namespace KPIS.GERP.GAMS.DAL
{
    public class D_ACM_INVENTORY_Control
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        
        public IList<M_ACM_INVENTORY_Control> GetList()
        {
            DataRowCollection drc = null;
            IList<M_ACM_INVENTORY_Control> infoList = new List<M_ACM_INVENTORY_Control>();
            try
            {
                dbManager.Open();
                //string strSQL =   " SELECT ResGrpId,Code,Name "
                //                + " ,Descr,ResTypeId,AssetAccountId "
                //                + " ,CumdepreAccountId,DepreAccountId,ConsumeAccountId "
                //                + " ,BreakAccountId,record_status,updated_by,updated_when "
                //                + " FROM IcmMasResourceGroup "
                //                + " WHERE record_status not like 'D' ";

                string strSQL = " SELECT inv.ResGrpId,inv.Code,inv.Name,inv.Descr "
                               + " ,inv.ResTypeId,inv.AssetAccountId,inv.CumdepreAccountId "
                               + " ,inv.DepreAccountId,inv.ConsumeAccountId,inv.BreakAccountId "
                               + " ,concat(coalesce(acc.account_code, ''), ' : ', coalesce(acc.account_name, '')) AS asset_acc_name "
                               + " ,concat(coalesce(acc1.account_code, ''), ' : ', coalesce(acc1.account_name, '')) AS cumdepre_acc_name "
                               + " ,concat(coalesce(acc2.account_code, ''), ' : ', coalesce(acc2.account_name, '')) AS consume_acc_name "
                               + " ,concat(coalesce(acc3.account_code, ''), ' : ', coalesce(acc3.account_name, '')) AS depre_acc_name "
                               + " ,concat(coalesce(acc4.account_code, ''), ' : ', coalesce(acc4.account_name, '')) AS break_acc_name "
                               + " FROM IcmMasResourceGroup AS inv "
                               + " Left Join acm_mas_account AS acc ON inv.AssetAccountId = acc.account_seq "
                               + " Left Join acm_mas_account AS acc1 ON inv.CumdepreAccountId = acc1.account_seq "
                               + " Left Join acm_mas_account AS acc2 ON acc2.account_seq = inv.ConsumeAccountId "
                               + " Left Join acm_mas_account AS acc3 ON inv.DepreAccountId = acc3.account_seq "
                               + " Left Join acm_mas_account AS acc4 ON inv.BreakAccountId = acc4.account_seq "
                               + " WHERE inv.RecordStatus not like  'D' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ACM_INVENTORY_Control info = new M_ACM_INVENTORY_Control();
                        info.resource_seq = Convert.ToInt32(drc[i]["ResGrpId"]);
                        info.resource_code = drc[i]["Code"].ToString();
                        info.resource_name = drc[i]["Name"].ToString();
                        info.resource_descr = drc[i]["Descr"].ToString();
                        info.AssetAccountId = cn.ConvertNullInt(drc[i]["AssetAccountId"]);
                        info.BreakAccountId = cn.ConvertNullInt(drc[i]["BreakAccountId"]);
                        info.DepreAccountId = cn.ConvertNullInt(drc[i]["DepreAccountId"]);
                        info.ConsumeAccountId = cn.ConvertNullInt(drc[i]["ConsumeAccountId"]);
                        info.CumdepreAccountId = cn.ConvertNullInt(drc[i]["CumdepreAccountId"]);
                        info.asset_account_name = drc[i]["asset_acc_name"].ToString();
                        info.cumdepre_account_name = drc[i]["cumdepre_acc_name"].ToString();
                        info.depre_account_name = drc[i]["depre_acc_name"].ToString();
                        info.consume_account_name = drc[i]["consume_acc_name"].ToString();
                        info.break_account_name = drc[i]["break_acc_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
           

        }
        public IList<M_ACM_INVENTORY_Control> GetListFillByType(Nullable<Int32> resource_type)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ACM_INVENTORY_Control> infoList = new List<M_ACM_INVENTORY_Control>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@resource_type", resource_type);

                strSQL = " SELECT inv.ResGrpId, inv.Code, inv.Name, inv.Descr"
                          + " , inv.ResTypeId, inv.AssetAccountId, inv.CumdepreAccountId"
                          + " , inv.DepreAccountId,inv.ConsumeAccountId,inv.BreakAccountId"
                          + " , concat(coalesce(acc.account_code, ''), ' : ', coalesce(acc.account_name, '')) AS asset_acc_name"
                          + " , concat(coalesce(acc1.account_code, ''), ' : ', coalesce(acc1.account_name, '')) AS cumdepre_acc_name"
                          + " , concat(coalesce(acc2.account_code, ''), ' : ', coalesce(acc2.account_name, '')) AS consume_acc_name"
                          + " , concat(coalesce(acc3.account_code, ''), ' : ', coalesce(acc3.account_name, '')) AS depre_acc_name"
                          + " , concat(coalesce(acc4.account_code, ''), ' : ', coalesce(acc4.account_name, '')) AS break_acc_name"
                          + " FROM IcmMasResourceGroup AS inv "
                          + " Left Join acm_mas_account AS acc ON inv.AssetAccountId = acc.account_seq "
                          + " Left Join acm_mas_account AS acc1 ON inv.CumdepreAccountId = acc1.account_seq "
                          + " Left Join acm_mas_account AS acc2 ON acc2.account_seq = inv.ConsumeAccountId "
                          + " Left Join acm_mas_account AS acc3 ON inv.DepreAccountId = acc3.account_seq "
                          + " Left Join acm_mas_account AS acc4 ON inv.BreakAccountId = acc4.account_seq "
                          + " WHERE inv.RecordStatus not like  'D' "
                          + " AND ResTypeId =  @resource_type ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ACM_INVENTORY_Control info = new M_ACM_INVENTORY_Control();
                        info.resource_seq = Convert.ToInt32(drc[i]["ResGrpId"]);
                        info.resource_code = drc[i]["Code"].ToString();
                        info.resource_name = drc[i]["Name"].ToString();
                        info.resource_descr = drc[i]["Descr"].ToString();
                        info.AssetAccountId = cn.ConvertNullInt(drc[i]["AssetAccountId"]);
                        info.BreakAccountId = cn.ConvertNullInt(drc[i]["BreakAccountId"]);
                        info.DepreAccountId = cn.ConvertNullInt(drc[i]["DepreAccountId"]);
                        info.ConsumeAccountId = cn.ConvertNullInt(drc[i]["ConsumeAccountId"]);
                        info.CumdepreAccountId = cn.ConvertNullInt(drc[i]["CumdepreAccountId"]);
                        info.asset_account_name = drc[i]["asset_acc_name"].ToString();
                        info.cumdepre_account_name = drc[i]["cumdepre_acc_name"].ToString();
                        info.depre_account_name = drc[i]["depre_acc_name"].ToString();
                        info.consume_account_name = drc[i]["consume_acc_name"].ToString();
                        info.break_account_name = drc[i]["break_acc_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }


        public M_ACM_INVENTORY_Control GetListFillBySeq(Nullable<Int32> resource_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ACM_INVENTORY_Control info = new M_ACM_INVENTORY_Control();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@resource_seq", resource_seq);

                     strSQL =   " SELECT inv.ResGrpId,inv.Code,inv.Name,inv.Descr "
                               + " ,inv.ResTypeId,inv.AssetAccountId,inv.CumdepreAccountId "
                               + " ,inv.DepreAccountId,inv.ConsumeAccountId,inv.BreakAccountId "
                               + " ,acc.account_name AS asset_acc_name,acc1.account_name AS cumdepre_acc_name "
                               + " ,acc2.account_name AS consume_acc_name,acc3.account_name AS depre_acc_name "
                               + " ,acc4.account_name AS break_acc_name "
                               + " FROM IcmMasResourceGroup AS inv "
                               + " Left Join acm_mas_account AS acc ON inv.AssetAccountId = acc.account_seq "
                               + " Left Join acm_mas_account AS acc1 ON inv.CumdepreAccountId = acc1.account_seq "
                               + " Left Join acm_mas_account AS acc2 ON acc2.account_seq = inv.ConsumeAccountId "
                               + " Left Join acm_mas_account AS acc3 ON inv.DepreAccountId = acc3.account_seq "
                               + " Left Join acm_mas_account AS acc4 ON inv.BreakAccountId = acc4.account_seq "
                               + " WHERE inv.RecordStatus not like  'D' "
                               + " AND ResGrpId = @resource_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.resource_seq = Convert.ToInt32(drc[0]["ResGrpId"]);
                    info.resource_code = drc[0]["Code"].ToString();
                    info.resource_name = drc[0]["Name"].ToString();
                    info.resource_descr = drc[0]["Descr"].ToString();
                    info.AssetAccountId = cn.ConvertNullInt(drc[0]["AssetAccountId"]);
                    info.BreakAccountId = cn.ConvertNullInt(drc[0]["BreakAccountId"]);
                    info.DepreAccountId = cn.ConvertNullInt(drc[0]["DepreAccountId"]);
                    info.ConsumeAccountId = cn.ConvertNullInt(drc[0]["ConsumeAccountId"]);
                    info.CumdepreAccountId = cn.ConvertNullInt(drc[0]["CumdepreAccountId"]);
                    info.asset_account_name = drc[0]["asset_acc_name"].ToString();
                    info.cumdepre_account_name = drc[0]["cumdepre_acc_name"].ToString();
                    info.depre_account_name = drc[0]["depre_acc_name"].ToString();
                    info.consume_account_name = drc[0]["consume_acc_name"].ToString();
                    info.break_account_name = drc[0]["break_acc_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        /// <summary>
        /// MODIFY วัสดุใช้ไป
        /// </summary>
        public void UPDATE(M_ACM_INVENTORY_Control info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);
                dbManager.AddParameters(0, "resource_seq", info.resource_seq);
                dbManager.AddParameters(1, "@AssetAccountId", info.AssetAccountId);
                dbManager.AddParameters(2, "@CumdepreAccountId", info.CumdepreAccountId);
                dbManager.AddParameters(3, "@DepreAccountId", info.DepreAccountId);
                dbManager.AddParameters(4, "@ConsumeAccountId", info.ConsumeAccountId);
                dbManager.AddParameters(5, "@BreakAccountId", info.BreakAccountId);
                dbManager.AddParameters(6, "@updated_by", info.updated_by);
                dbManager.AddParameters(7, "@updated_when", info.updated_when);

                strSQL = "UPDATE IcmMasResourceGroup"
                    + "  SET AssetAccountId = @AssetAccountId "
                    + "  ,CumdepreAccountId = @CumdepreAccountId "
                    + "  ,DepreAccountId = @DepreAccountId "
                    + "  ,ConsumeAccountId = @ConsumeAccountId "
                    + "  ,BreakAccountId = @BreakAccountId "
                    + "  ,UpdatedBy = @updated_by"
                    + "  ,UpdatedWhen = @updated_when"
                    + "  WHERE ResGrpId = @resource_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public DataTable Report_INVENTORY_Control()
        {
            string strSQL;
            DataTable dtb = null;
            try
            {
                dbManager.Open();
                //dbManager.CreateParameters(1);
                //dbManager.AddParameters(0, "@ResTypeId", ResTypeId);

                strSQL =  " SELECT inv.ResGrpId,inv.Code as icm_res_grp_code, inv.Name as icm_res_grp_name, inv.Descr as icm_res_grp_descr "
                        + " ,inv.ResTypeId,inv.AssetAccountId,inv.CumdepreAccountId "
                        + " ,inv.DepreAccountId,inv.ConsumeAccountId,inv.BreakAccountId "
                        + " ,acc.account_name AS asset_acc_name,acc1.account_name AS cumdepre_acc_name "
                        + " ,acc2.account_name AS consume_acc_name,acc3.account_name AS depre_acc_name "
                        + " ,acc4.account_name AS break_acc_name "
                        + " ,case  inv.ResTypeId when 1 then 'วัสดุ' "
                        + " when 2 then 'ครุภัณฑ์' "
                        + " when 3 then 'ที่ดิน' "
                        + " when 4 then 'สิ่งก่อสร้าง' "
                        + " end res_name "
                        + " ,acc.account_code as asset_acc_code, acc1.account_code as cumdepre_acc_code "
                        + " ,acc2.account_code as consume_acc_code,acc3.account_code as depre_acc_code "
                        + " ,acc4.account_code as break_acc_code "
                        + " FROM IcmMasResourceGroup AS inv "
                        + " Left Join acm_mas_account AS acc ON inv.AssetAccountId = acc.account_seq "
                        + " Left Join acm_mas_account AS acc1 ON inv.CumdepreAccountId = acc1.account_seq "
                        + " Left Join acm_mas_account AS acc2 ON acc2.account_seq = inv.ConsumeAccountId "
                        + " Left Join acm_mas_account AS acc3 ON inv.DepreAccountId = acc3.account_seq "
                        + " Left Join acm_mas_account AS acc4 ON inv.BreakAccountId = acc4.account_seq "
                        + " WHERE inv.RecordStatus not like  'D' "
                        + " ORDER BY res_name ";
                // + " AND ResTypeId =  @ResTypeId "

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return dtb;
        }

   

    }
}